package com.uchengwang.ui.cm_ui.ucity.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.entity.MapRequest;
import com.uchengwang.functions.http.listener.HttpCallBack;
import com.uchengwang.functions.http.listener.IHttpManager;
import com.uchengwang.functions.http.listener.OnResultListener;
import com.uchengwang.functions.http.manager.HttpManager;
import com.uchengwang.ui.base_ui.ui.BaseWithTitleActivity;
import com.uchengwang.ui.cm_ui.cycle.bean.H5ReportBean;
import com.uchengwang.ui.cm_ui.me.presenter.HttpUserManager;
import com.uchengwang.ui.utils.LayoutAddViewUtils;
import com.uchengwang.ui.utils.LoadingUtils;
import com.uchengwang.ui.utils.UCommonUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by U城网 on 2017/7/20.
 * 举报界面
 */

public class ReportActivity extends BaseWithTitleActivity{
    //20举报个人 21举报群  9.校吧发帖举报／  8社团分享举报  .U城发布内容举报[各自对用type] 17举报社团纳新 16举报话题评论
    private int type;
    private long id;//举报内容的ID/人ID
    private String groupId;//群ID

    private LinearLayout fast_content;
    private String[] reportArray = null;
    private List<String> stringList;
    private H5ReportBean reportBean;

    @Override
    protected void onCreate(Bundle savedInstanceState)  {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_fast_release);
        type = getIntent().getIntExtra(BaseConfig.TYPE, -1);
        if (type ==BaseConfig.REPORT_GROUP_TYPE){
            groupId = getIntent().getStringExtra(BaseConfig.GROUP_ID);
        }else {
            id = getIntent().getLongExtra(BaseConfig.ID, -1);
            reportBean = (H5ReportBean) getIntent().getSerializableExtra(BaseConfig.BEAN);
        }

        initView();
    }

    private void initView(){
        fast_content = (LinearLayout) findViewById(R.id.fast_content);

        switch (type){
            case BaseConfig.REPORT_PERSON_TYPE:
                setTitle("举报个人");
                reportArray = new String[]{"盗号冒充", "诽谤侮辱", "淫秽色情", "违法诈骗", "其他"};
                break;
            case BaseConfig.REPORT_GROUP_TYPE:
                setTitle("举报群");
                reportArray = new String[]{"非法活动", "敏感问题", "淫秽色情", "诈骗行为"};
                break;
            case BaseConfig.REPORT_CYCLE_TYPE:
                setTitle("举报");
                reportArray = new String[]{"色情暴力", "违法活动", "政治敏感", "侮辱诽谤", "其他"};
                break;

        }

        initData();

    }

    private void initData() {

        stringList = new ArrayList<>();
        if (reportArray!= null && reportArray.length >0){
            for (int i=0; i<reportArray.length; i++){
                stringList.add(reportArray[i]);
            }
        }


        new LayoutAddViewUtils(this).addView(fast_content, stringList).setOnViewItemListener(new LayoutAddViewUtils.OnViewItemListener() {
            @Override
            public void onViewItem(View view, int position) {
                String title = stringList.get(position);

                switch (type){
                    case BaseConfig.REPORT_CYCLE_TYPE:
                        report(title);
                        break;

                    case BaseConfig.REPORT_PERSON_TYPE:
                        HttpUserManager.getManager().reportPeople(ReportActivity.this, id, title, new OnResultListener() {
                            @Override
                            public void onResult(int stutas, String msg, int total, String data) {
                                if (stutas ==0){
                                    UCommonUtil.getUtil().showToast(ReportActivity.this, "举报成功");
                                    ReportActivity.this.finish();
                                }else {
                                    UCommonUtil.getUtil().showToast(ReportActivity.this, "举报失败");
                                }
                            }
                        });
                        break;
                }
//                if (type != BaseConfig.REPORT_CYCLE_TYPE){
//                    Intent intent = new Intent(ReportActivity.this,  ReportDetailActivity.class);
//                    intent.putExtra(BaseConfig.TITLE, title);
//                    if (type ==BaseConfig.REPORT_GROUP_TYPE){
//                        intent.putExtra(BaseConfig.GROUP_ID, groupId);
//                    }else {
//                        intent.putExtra(BaseConfig.ID, id);
//                    }
//                    intent.putExtra(BaseConfig.TYPE, type);
//                    startActivity(intent);
//                    ReportActivity.this.finish();
//                }else {
//
//                    report(title);
//                }

            }
        });

    }

    //举报社圈内容
    private void report(String reason){
        if (reportBean == null){
            return;
        }
        LoadingUtils.getUtils().loading(this);
        MapRequest map = new MapRequest();
        map.put(BaseConfig.REFER_ID, reportBean.getReferId());
        map.put(BaseConfig.REFER_TYPE, reportBean.getReferType());
        map.put(BaseConfig.REPORT_UID, reportBean.getReportUid());
        map.put(BaseConfig.REPORT_NICK_NAME, reportBean.getReportNickName());
        map.put(BaseConfig.REPORT_USER_AVATAR, reportBean.getReportUserAvatar());
        map.put(BaseConfig.REPORT_REASON, reason);
        HttpManager.newInstance(this).query(IHttpManager.RequestMethod.POST, "/report", map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                LoadingUtils.getUtils().hide();
                UCommonUtil.getUtil().showToast(ReportActivity.this, msg);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                LoadingUtils.getUtils().hide();
                if (status ==0 ){
                    UCommonUtil.getUtil().showToast(ReportActivity.this, "举报成功, 等待平台审核");
                    ReportActivity.this.finish();
                }else {
                    UCommonUtil.getUtil().showToast(ReportActivity.this, msg);
                }
            }
        });
    }
}
